package cn.elane.kibana.demo.global.model;

import cn.elane.kibana.demo.global.util.CloneUtil;
import cn.elane.kibana.demo.global.util.Converter;
import cn.elane.kibana.demo.global.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author Chenyu
 * @date 2019-10-29
 */
public class NameBean extends HashMap<String, Object> {

	private Logger logger = LoggerFactory.getLogger(NameBean.class.getName());

	public NameBean() {
	}

	/**
	 * 初始化对象.
	 *
	 * @param jsonStr json格式字符串.
	 */
	public NameBean(String jsonStr) {
		this.init(jsonStr);
	}

	/**
	 * 初始化值容器
	 * <br>去掉换行符/回车,制表符统一为四个空格.
	 *
	 * @param jsonStr JSON格式字符串.
	 * @return 当前值容器
	 */
	public NameBean init(String jsonStr) {
		if (!StringUtil.ifJSONObjectStr(jsonStr)) {
			return this;
		}
		String str = jsonStr.replaceAll("[\n\r]", "").replaceAll("[\t]", "    ");
		JSONObject json = JSONObject.parseObject(str.trim());
		if (json == null) {
			return this;
		}
		return this.initBean(json.getInnerMap());
	}

	public NameBean init(Map<? extends String, ?> map) {
		if (map == null || map.isEmpty()) {
			return this;
		}
		return this.initBean(map);
	}

	private NameBean initBean(Map<? extends String, ?> map) {
		if (map == null || map.isEmpty()) {
			return this;
		}
		Object v;
		String className;
		for (String k : map.keySet()) {//TODO:此处考虑使用entry处理.可参考toJson();
			v = map.get(k);
			if (v == null) {
				this.put(k, "");
				continue;
			}
			className = v.getClass().getName();
			if (className.equals(Byte.class.getName())) {
				this.put(k, ((Byte) v));
			} else if (className.equals(Short.class.getName())) {
				this.put(k, ((Short) v));
			} else if (className.equals(Integer.class.getName())) {
				this.put(k, ((Integer) v));
			} else if (className.equals(Float.class.getName())) {
				this.put(k, ((Float) v));
			} else if (className.equals(Double.class.getName())) {
				this.put(k, ((Double) v));
			} else if (className.equals(Long.class.getName())) {
				this.put(k, ((Long) v));
			} else if (className.equals(Boolean.class.getName())) {
				this.put(k, (Boolean) v);
			} else if (v instanceof Date) {
				this.put(k, TypeUtils.castToDate(v));
			} else if (v instanceof NameBean) {
				this.put(k, (NameBean) v);
			} else if (v instanceof BigDecimal) {
				this.put(k, TypeUtils.castToBigDecimal(v));
			} else {
				String value = v.toString().trim();
				try {
					if (StringUtil.ifJSONObjectStr(value)) {
						this.put(k, new NameBean(value));
					} else if (StringUtil.ifJSONArrayStr(value)) {
						this.put(k, NameBeanUtil.init2List(value));
					} else if (value.startsWith("[") && value.endsWith("]") && !value.startsWith("[{")) {
						if (value.length() > 2) {
							Object[] objRs = value.substring(1, value.length() - 1).split(",");
							this.put(k, objRs);
						} else {
							this.put(k, new byte[]{});
						}
					} else {
						this.put(k, v);
					}
				} catch (Exception e) {
					logger.error(e.getMessage());
					this.put(k, v);
				}
			}
		}
		return this;
	}

	public NameBean put(String name, Byte value) {
		if (value == null) {
			value = (byte) 0;
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, Short value) {
		if (value == null) {
			value = (short) 0;
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, Integer value) {
		if (value == null) {
			value = 0;
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, Long value) {
		if (value == null) {
			value = (long) 0;
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, Float value) {
		if (value == null) {
			value = (float) 0;
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, Double value) {
		if (value == null) {
			value = (double) 0;
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, Boolean value) {
		if (value == null) {
			value = false;
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, BigDecimal value) {
		if (value == null) {
			value = BigDecimal.valueOf(0);
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, String value) {
		if (value == null) {
			value = "";
		}
		return this.putVal(name, value);
	}

	public NameBean put(String name, NameBean bean) {
		return this.putVal(name, bean);
	}

	public NameBean put(String name, List<NameBean> beanList) {
		return this.putVal(name, beanList);
	}

	public NameBean put(String name, Date value) {
		return this.putVal(name, value);
	}

	@Override
	public NameBean put(String name, Object value) {
		return this.putVal(name, value);
	}

	@Override
	public void putAll(Map<? extends String, ?> bean) {
		this.init(bean);
	}

	/**
	 * 基于原有name的值为<b>Bean</b>或<b>List(Bean)</b>的附加<br>
	 * 如果原有值为NULL或非Bean、List(Bean),那么name的值为:(Bean)value<br>
	 * 如果原有值为Bean,那么name的值为:原有Bean与value数据结构相同的:原有Bean加上value生成的List(Bean),原有Bean与value数据结构不相同的,将value的值append到原Bean中<br>
	 * 如果原有值为List(Bean),那么name的值为:原有List(Bean)加上value的List(Bean)<br>
	 *
	 * @param name 原有key
	 * @param bean 待附加的Bean
	 * @return Bean
	 */
	public NameBean add(String name, NameBean bean) {
		if (bean == null) {
			return this;
		}
		try {
			NameBean value = bean.copy();
			Object obj = this.obj(name);
			if (obj == null) {
				List<NameBean> list = new ArrayList<>(1);
				list.add(bean);
				this.put(name, list);
			} else if (obj instanceof List) {
				List<NameBean> list = this.beans(name);
				list.add(value);
				this.put(name, list);
            /*} else {
                this.put(name, value);*/
			}
			return this;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return this;
		}
	}

	/**
	 * 如果结果为NULL将返回默认值0
	 *
	 * @param name key
	 * @return Byte or 0
	 */
	public Byte getByte(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof Byte) {
					return (Byte) obj;
				} else if (obj instanceof String) {
					return Byte.valueOf(obj.toString());
				}
			}
			return 0;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return 0;
		}
	}

	/**
	 * 获取Byte
	 *
	 * @param name       key
	 * @param defaultVal 默认值
	 * @return Byte or 0
	 */
	public Byte getByte(String name, Byte defaultVal) {
		Byte rs = getByte(name);
		if (rs == null) {
			return defaultVal;
		}
		return rs;
	}

	public Short getShort(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof Short) {
					return Short.valueOf(obj.toString());
				} else {
					return 0;
				}
			}
			return 0;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return 0;
		}
	}

	public Short getShort(String name, Short defaultVal) {
		Short rs = getShort(name);
		if (rs == null) {
			return defaultVal;
		}
		return rs;
	}

	public Integer getInteger(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof Integer) {
					return (Integer) obj;
				} else if (obj instanceof String) {
					return Integer.valueOf(obj.toString());
				}
			}
			return 0;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return 0;
		}
	}

	/**
	 * 获取整形
	 *
	 * @param name       Key
	 * @param defaultVal 默认值
	 * @return
	 */
	public Integer getInteger(String name, Integer defaultVal) {
		Integer rs = getInteger(name);
		if (rs == null) {
			return defaultVal;
		}
		return rs;
	}


	public Long getLong(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof Long) {
					return (Long) obj;
				} else if (obj instanceof String) {
					return Long.valueOf(obj.toString());
				}
			}
			return 0L;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return 0L;
		}
	}

	/**
	 * 获取长整形
	 *
	 * @param name       Key
	 * @param defaultVal 默认值
	 * @return
	 */
	public Long getLong(String name, Long defaultVal) {
		Long rs = getLong(name);
		if (rs == null) {
			return defaultVal;
		}
		return rs;
	}

	public Float getFloat(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof Float) {
					return (Float) obj;
				} else if (obj instanceof String) {
					return Float.valueOf(obj.toString());
				}
			}
			return 0F;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return 0F;
		}
	}

	/**
	 * 获取单精度
	 *
	 * @param name       Key
	 * @param defaultVal 默认值
	 * @return
	 */
	public Float getFloat(String name, Float defaultVal) {
		Float rs = getFloat(name);
		if (rs == null) {
			return defaultVal;
		}
		return rs;
	}

	public Double getDouble(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof Double) {
					return (Double) obj;
				} else if (obj instanceof String) {
					return Double.valueOf(obj.toString());
				}
			}
			return (double) 0;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return (double) 0;
		}
	}

	/**
	 * 获取双精度
	 *
	 * @param name
	 * @param defaultVal
	 * @return
	 */
	public Double getDouble(String name, Double defaultVal) {
		Double rs = getDouble(name);
		if (rs == null) {
			return defaultVal;
		}
		return rs;
	}

	public BigDecimal getBigDecimal(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof BigDecimal) {
					return (BigDecimal) obj;
				} else if (obj instanceof String) {
					return Converter.toBigDecimal(obj, BigDecimal.valueOf(0));
				}
			}
			return BigDecimal.valueOf(0);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return BigDecimal.valueOf(0);
		}
	}

	/**
	 * 获取多精度
	 *
	 * @param name       Key
	 * @param defaultVal 默认值
	 * @return
	 */
	public BigDecimal getBigDecimal(String name, BigDecimal defaultVal) {
		BigDecimal rs = getBigDecimal(name);
		if (rs == null) {
			return defaultVal;
		}
		return rs;
	}

	/**
	 * 获取多精度
	 *
	 * @param name       Key
	 * @param defaultVal 默认值
	 * @return
	 */
	public BigDecimal getBigDecimal(String name, String defaultVal) {
		BigDecimal rs = getBigDecimal(name);
		if (rs == null) {
			return new BigDecimal(defaultVal == null ? "0" : defaultVal);
		}
		return rs;
	}

	/**
	 * @param name key
	 * @return 真实结果(含null)
	 */
	public String getString(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof String) {
					return (String) obj;
				} else {
					return obj.toString();
				}
			}
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	/**
	 * 获取字符串
	 *
	 * @param name       Key
	 * @param defaultVal 默认值
	 * @return
	 */
	public String getString(String name, String defaultVal) {
		String rs = getString(name);
		if (rs == null) {
			return defaultVal;
		}
		return rs;
	}

	public Date getDate(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof Date) {
					return (Date) obj;
				} else if (obj instanceof String) {
					return Converter.toDate(obj);
				}
			}
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	/**
	 * 返回当前对象的的Map集合
	 * @return Map<String, Object>
	 */
    /*public Map<String, Object> getMap() {
        return this.map;
    }*/

	/**
	 * 字符串数组处理成字符串.
	 *
	 * @param data p
	 * @return r
	 */
	private String getArrayStr(String[] data) {
		StringBuilder sb = new StringBuilder();
		if (data != null && data.length > 0) {
			sb.append("[");
			for (String s : data) {
				sb.append("\"").append(s).append("\",");
			}
		}
		if (sb.toString().endsWith(",")) {
			sb.delete(sb.length() - 1, sb.length());
		}
		sb.append("]");
		return sb.toString();
	}

	/**
	 * 数组中可能包含引用数据类型.
	 * <br>此处可处理List/NameBean
	 *
	 * @param data p
	 * @return r
	 */
	private String getArrayStr(Object[] data) {
		StringBuilder sb = new StringBuilder();
		if (data != null && data.length > 0) {
			sb.append("[");
			for (Object s : data) {
				if (s instanceof List) {
					sb.append(NameBeanUtil.toJsonObj((List<Object>) s));
				} else if (s instanceof NameBean) {
					sb.append(((NameBean) s).toJson());
				} else if (StringUtil.ifJSONObjectStr(s.toString())) {
					sb.append(s);
				} else {
					//TODO:此处可进行整型转换判断处理.可处理,但还未校验性能方面问题.
                    /*try {
                        Integer rs = Integer.valueOf(s.toString());
                        sb.append(rs);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                        sb.append("\"").append(s).append("\"");
                    }*/
					sb.append("\"").append(s).append("\"");
				}
				sb.append(",");
			}
		}
		if (sb.toString().endsWith(",")) {
			sb.delete(sb.length() - 1, sb.length());
		}
		sb.append("]");
		return sb.toString();
	}

	/**
	 * 结果为NULL的以空字符串返回<br/>
	 * 该接口可将其它类型转换为String返回
	 *
	 * @param name key
	 * @return 字符串
	 */
	public String str(String name) {
		try {
			Object obj = this.obj(name);
			if (obj == null) {
				return "";
			} else if (obj instanceof NameBean) {
				return ((NameBean) obj).toJson();
			} else if (obj instanceof Map) {
				return this.bean(name).toJson();
			} else if (obj instanceof List) {
				return NameBeanUtil.toJsonObj((List<Object>) obj);
                /*List<NameBean> list = this.beans(name);
                if (list == null) {
                    return this.obj(name).toString();
                }
                return NameBeanUtil.toJson(list);*/
			} else if (obj instanceof Object[]) {
				return Arrays.toString((String[]) obj);
			} else if (obj instanceof String && StringUtil.ifJSONObjectStr(obj.toString())) {
				return new NameBean(obj.toString()).toJson();
			} else {
				return String.valueOf(obj);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "";
		}
	}

	/**
	 * 结果为[NULL/空字符串]的以[defaultVal]返回<br/>
	 * 该接口可将其它类型转换为String返回
	 *
	 * @param name       key
	 * @param defaultVal 当值为null时,返回defaultValue
	 * @return 字符串
	 */
	public String str(String name, String defaultVal) {
		String str = str(name);
		if (str.length() == 0) {
			return defaultVal;
		}
		return str(name);
	}

	public NameBean bean(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof NameBean) {
					return (NameBean) obj;
				} else if (obj instanceof Map) {
					return new NameBean().init((Map<String, Object>) obj);
				} else if (StringUtil.ifJSONObjectStr(obj.toString())) {
					return new NameBean(obj.toString());
				}
			}
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	/**
	 * 获取NameBean集合.<br>如果集合中的元素为Map/NameBean,统一范围List&lt;NameBean&gt;
	 * <br>如果集合中的元素为其它,那么会将List中的元素转为一个NameBean返回.返回的集合中只有这一个NameBean.并且key为1-N
	 *
	 * @param name name
	 * @return
	 */
	public List<NameBean> beans(String name) {
		try {
			Object obj = this.obj(name);
			if (obj != null) {
				if (obj instanceof List) {
					List<Object> list = (List<Object>) obj;
					if (list.size() > 0 && list.get(0) != null) {
						Object temp = list.get(0);
						if (temp instanceof NameBean) {
							return (List<NameBean>) obj;
						} else if (temp instanceof Map) {
							return NameBeanUtil.mapList2NameBeanList((List<Map<String, Object>>) obj);
						} else {
							List<NameBean> rs = new ArrayList<>(1);
							NameBean rsNB = new NameBean();
							int i = 0;
							for (Object o : list) {
								rsNB.put(++i + "", o);
							}
							rs.add(rsNB);
							return rs;
						}
					}
					return new ArrayList<>();
				} else if (StringUtil.ifJSONArrayStr(obj.toString())) {
					return NameBeanUtil.init2List(obj.toString());
				} else {
					throw new RuntimeException("类型错误!");
				}
			}
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	/**
	 * 该方法取原生对象.
	 */
	public Object obj(String name) {
		if (StringUtils.isEmpty(name)) {
			return null;
		}
		int index = name.indexOf(".");
		if (index == -1) {
			return get(name);
		} else {
			String sup = name.substring(0, index);
			String sub = name.substring(index + 1);
			Object obj = this.obj(sup);
			if (obj == null) {
				return null;
			} else if (obj instanceof NameBean) {
				return ((NameBean) obj).obj(sub);
			} else if (obj instanceof Map) {
				return this.bean(sup).obj(sub);
			} else if (obj instanceof String && StringUtil.ifJSONObjectStr(obj.toString())) {
				return new NameBean(obj.toString()).obj(sub);
			} else {
				return null;
			}
		}
	}

	@Override
	public String toString() {
		return toJson();
	}

	public String toJson() {
		StringBuilder json = new StringBuilder("{");
		if (!super.isEmpty()) {
			String key;
			Object val;
			for (Entry<String, Object> e : super.entrySet()) {
				key = e.getKey();
				val = e.getValue();
				if (val == null) {
					json.append("\"").append(key).append("\":\"\"");
				} else if (val instanceof Integer || val instanceof Byte || val instanceof Short) {
					json.append("\"").append(key).append("\":").append(Converter.toInteger(val, 0));
				} else if (val instanceof Long) {
					json.append("\"").append(key).append("\":").append(Converter.toLong(val, (long) 0));
				} else if (val instanceof Float) {
					json.append("\"").append(key).append("\":").append(Converter.toFloat(val, (float) 0));
				} else if (val instanceof Double) {
					json.append("\"").append(key).append("\":").append(Converter.toDouble(val, (double) 0));
				} else if (val instanceof Boolean) {
					json.append("\"").append(key).append("\":").append(Converter.toBoolean(val, false));
				} else if (val instanceof BigDecimal) {
					json.append("\"").append(key).append("\":").append(Converter.toBigDecimal(val, BigDecimal.valueOf(0)));
				} else if (val instanceof Date) {
					json.append("\"").append(key).append("\":\"").append(Converter.toDate(val)).append("\"");
				} else if (val instanceof NameBean) {
					json.append("\"").append(key).append("\":").append(((NameBean) val).toJson());
				} else if (val instanceof List) {
					json.append("\"").append(key).append("\":");
					try {
						json.append(NameBeanUtil.toJsonObj((List<Object>) val));
					} catch (Exception ex) {
						json.append("\"").append((Converter.toString(val, "").replace("\\", "\\\\")).replace("\"", "\\\"").replace("\r", " ").replace("\t", "    ").replace("\n", " ")).append("\"");
					}
				} else if (val.getClass().isArray()) {
					if (val instanceof byte[]) {
						json.append("\"").append(key).append("\":").append(Arrays.toString((byte[]) val));
					} else if (val instanceof short[]) {
						json.append("\"").append(key).append("\":").append(Arrays.toString((short[]) val));
					} else if (val instanceof int[]) {
						json.append("\"").append(key).append("\":").append(Arrays.toString((int[]) val));
					} else if (val instanceof long[]) {
						json.append("\"").append(key).append("\":").append(Arrays.toString((long[]) val));
					} else if (val instanceof float[]) {
						json.append("\"").append(key).append("\":").append(Arrays.toString((float[]) val));
					} else if (val instanceof double[]) {
						json.append("\"").append(key).append("\":").append(Arrays.toString((double[]) val));
					} else if (val instanceof boolean[]) {
						json.append("\"").append(key).append("\":").append(Arrays.toString((boolean[]) val));
					} else if (val instanceof char[]) {
						json.append("\"").append(key).append("\":\"").append(Arrays.toString((char[]) val)).append("\"");
					} else {
						json.append("\"").append(key).append("\":").append(getArrayStr((Object[]) val));
					}
				} else {
					json.append("\"").append(key).append("\":\"").append((Converter.toString(val, "").replace("\\", "\\\\")).replace("\"", "\\\"").replace("\r", " ").replace("\t", "    ").replace("\n", " ")).append("\"");
				}
				json.append(",");
			}
		}
		if (json.toString().endsWith(",")) {
			json.delete(json.length() - 1, json.length());
		}
		json.append("}");
		return json.toString();
	}

	/**
	 * 判断某个属性是否为[NULL/""]
	 *
	 * @param name key
	 * @return boolean
	 */
	public Boolean isEmpty(String name) {
		return "".equals(this.str(name));
	}

	/**
	 * 判断某个属性是否不为[NULL/""]
	 *
	 * @param name key
	 * @return boolean
	 */
	public Boolean notEmpty(String name) {
		return !"".equals(this.str(name));
	}

	/**
	 * 移除对应的值
	 *
	 * @param names key
	 * @return 移除后的Bean
	 */
	public NameBean remove(List<String> names) {
		if (names != null && names.size() > 0) {
			for (String n : names) {
				this.remove(n);
			}
		}
		return this;
	}

	/**
	 * 移除对应的值
	 *
	 * @param names key
	 * @return 移除后的Bean
	 */
	public NameBean remove(String... names) {
		if (names != null && names.length > 0) {
			for (String n : names) {
				this.remove(n);
			}
		}
		return this;
	}

	/**
	 * 根据names生成新的Bean进行返回
	 *
	 * @param names key列表
	 * @return Bean
	 */
	public NameBean cut(List<String> names) {
		NameBean rs = new NameBean();
		if (names != null && names.size() > 0) {
			for (String str : names) {
				rs.put(str, this.obj(str));
			}
		}
		return rs;
	}

	/**
	 * 根据names生成新的Bean进行返回
	 *
	 * @param names key列表
	 * @return Bean
	 */
	public NameBean cut(String... names) {
		NameBean rs = new NameBean();
		if (names != null && names.length > 0) {
			for (String str : names) {
				rs.put(str, this.obj(str));
			}
		}
		return rs;
	}

	public NameBean copy() {
		try {
			if (super.isEmpty()) {
				return new NameBean();
			}
			return CloneUtil.deepClone(this);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new NameBean(this.toJson());
		}
	}

	/**
	 * 基于Bean的附加,相同key会被覆盖!
	 *
	 * @param bean 待附加的Bean
	 * @return 附加完成后的Bean
	 */
	public NameBean append(NameBean bean) {
		if (bean == null || bean.isEmpty()) {
			return this;
		}
		for (String k : bean.keySet()) {
			this.put(k, bean.get(k));
		}
		return this;
	}

	/**
	 * 基于Bean的附加,追加到name下!
	 *
	 * @param name 追加到name中
	 * @param bean 待附加的Bean
	 * @return 附加完成后的Bean
	 */
	public NameBean append(String name, NameBean bean) {
		if (bean == null || bean.isEmpty()) {
			return this;
		}
		Object obj = this.obj(name);
		if (obj == null) {
			this.put(name, bean);
		} else if (obj instanceof NameBean) {
			this.put(name, ((NameBean) obj).copy().append(bean));
		} else if (obj instanceof String) {
			if (StringUtil.ifJSONObjectStr(obj.toString())) {
				this.put(name, new NameBean(obj.toString()).append(bean));
			} else {
				this.put(name, bean.copy().put(name, obj));
			}
		}
		return this;
	}

	/**
	 * 移除对应的值
	 *
	 * @param name key
	 * @return 移除后的Bean
	 */
	private NameBean remove(String name) {
		if (name == null || "".equals(name)) {
			return this;
		}
		if (name.trim().startsWith(".")) {
			name = name.trim().substring(1);
		}
		int index = name.indexOf(".");
		if (index == -1) {
			super.remove(name);
			return this;
		} else {
			String sup = name.substring(0, index);
			String sub = name.substring(index + 1);
			Object obj = this.obj(sup);
			if (obj == null) {
				return this;
			} else if (obj instanceof NameBean) {
				return ((NameBean) obj).remove(sub);
			} else {
				logger.error("当前实体: " + toJson());
				logger.error("Error: " + sup + "【并非Bean类型！】==>" + obj.getClass().getName());
				return this;
			}
		}
	}

	private NameBean putVal(String name, Object value) {
		if (name == null || "".equals(name)) {
			return this;
		}
		if (name.trim().startsWith(".")) {
			name = name.trim().substring(1);
		}
		int index = name.indexOf(".");
		if (index == -1) {
			super.put(name, value);
		} else {
			String key = name.substring(0, index);
			String left = name.substring(index + 1);
			Object obj = super.get(key);
			if (obj == null) {
				this.putVal(key, new NameBean().putVal(left, value));
			} else if (obj instanceof NameBean) {
				((NameBean) obj).putVal(left, value);
			} else if (obj instanceof String) {
				if (StringUtil.ifJSONObjectStr(obj.toString())) {
					this.putVal(key, new NameBean(obj.toString()).putVal(left, value));
				} else {
					this.putVal(key, new NameBean().put(key, obj).putVal(left, value));
				}
			}
		}
		return this;
	}
}
